package com.cheng.reggie.controller;

import com.cheng.reggie.common.R;
import com.cheng.reggie.dto.OrderDto;
import com.cheng.reggie.entity.Orders;
import com.cheng.reggie.entity.Page;
import com.cheng.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired(required = false)
    private OrderService orderService;
    
    @RequestMapping("/submit")
    public R<String> addOrder(@RequestBody Orders orders, HttpSession session){
        //获取登录id
        Long userId = (Long) session.getAttribute("user");
        //调用业务生成订单数据方法,把orders和userId封装进行
        orderService.addOrder(orders,userId);
        return R.success("订单成功!");
    }
    @GetMapping("/userPage")
    public  R<Page<OrderDto>> userPage(Integer page, Integer pageSize, HttpSession session) {
        //获取用户id
        Long uid = (Long) session.getAttribute("user");
        //调用业务获取分页数据
        Page<OrderDto> pageObj = orderService.findByPage(page, pageSize, uid);
        //返回数据
        return R.success(pageObj);
    }
}
